package com.mystore.servlet;

import com.mystore.service.GoodService;
import com.mystore.service.impl.GoodServiceImpl;
import com.mystore.util.PageUtil;
import com.mystore.vo.GoodVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "GoodServlet",value = "/check/good/*")
public class GoodServlet extends HttpServlet {
  GoodService goodService = new GoodServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String uri = request.getRequestURI();
       String method = uri.replace(request.getContextPath() + "/check/good/", "");
       System.out.println("method="+method);
        PrintWriter out = response.getWriter();
        String url = "";
        switch (method){
            case "getGoods"://获取相应商品种类下面的信息
                int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
                int pageSize = Integer.parseInt(request.getParameter("pageSize"));
                int gid = Integer.parseInt(request.getParameter("gid"));
                PageUtil<GoodVO> pageUtil = goodService.findAllGoodByGid(gid,pageSize,pageNumber);
                request.setAttribute("goods",pageUtil);
                request.setAttribute("gid",gid);
                url+= "/WEB-INF/page/showGoods.jsp";
                request.getRequestDispatcher(url).forward(request,response);
                break;
            default:
                    out.println("invalid mark");

        }





    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
